package com.base.common.action;

import static com.base.common.GlobaConstants.ACTION_RESULT;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_HTML;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.io.Serializable;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.base.common.bean.ActionResult;
import com.base.common.utils.StringUtils;
import com.cjy.common.utils.ApplicationUtils;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 基础Action
 * 
 * @author Kevin Colin
 */

@ParentPackage("basePackage")
@Namespace("/")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT, CONTENT_TYPE, CONTENT_TYPE_HTML }) })
public abstract class AbstractBaseAction extends ActionSupport implements
		ServletResponseAware, ServletRequestAware, SessionAware, Serializable {
	private static final long serialVersionUID = 1L;
	protected HttpServletResponse response;
	protected HttpServletRequest request;
	protected Map<String, Object> session;
	protected Logger logger = Logger.getLogger(Class.class);

	// 返回的结果集
	protected ActionResult actionResult;
	// 数据库ID，统一使用UUID
	protected String uuid;
	// 是否是异步请求(ajax),参数为 true/y/Y.... 如果是async请求，返回json数据，如果是正常请求，跳转页面
	private String async;

	/**
	 * 返回类型 ajaxSuccess/success 如果是ajax请求则返回ajaxSuccess,如果是普通请求则返回success
	 * 
	 * @return String
	 * @author jay.zhoujingjie
	 */
	protected String returnType() {
		if (StringUtils.isNotEmptyByTrim(async)) {
			if ("y".equalsIgnoreCase(async) || Boolean.parseBoolean(async)) {
				return ASYNCHRONOUS;
			} else {
				return SUCCESS;
			}
		}
		return ApplicationUtils.isAsyncRequest(request) ? ASYNCHRONOUS
				: SUCCESS;
	}

	public ActionResult getActionResult() {
		return actionResult;
	}

	public void setActionResult(ActionResult actionResult) {
		this.actionResult = actionResult;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
		this.request.getSession().setMaxInactiveInterval(60 * 60 * 24);
		// this.request.getSession().setMaxInactiveInterval(60);
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = StringUtils.encodeHTML(uuid);
	}

	public String getAsync() {
		return async;
	}

	public void setAsync(String async) {
		this.async = async;
	}

}
